/********************************************************************************
*Class:  SurveyTriggerPDA 
*Method: Search(),Save()
*Useage:Static methods  trigger survey by PDA.
*Notice: We could add email function( send email to every city manage and subordinate ).
*Author:huang xiaojiang
********************************************************************************/

public with sharing class SurveyTriggerPDA
{  
   
    //@future
    public static  void runByTrigger(list<TIMBASURVEYS__SurveyResponse__c> lstTriggerNew)
    {  
     
     set<id> sResId=new set<id>();//存放当前问题答案id
     Set<id> setRec=new Set<id>();//存放需要更新Recipient__c的id
     TIMBASURVEYS__Recipient__c oRecipient=null;
     
     list<TIMBASURVEYS__Survey_Summary__c> lstSurveySum=new  list<TIMBASURVEYS__Survey_Summary__c>();
     TIMBASURVEYS__Survey_Summary__c oSummary=null;
     list<TIMBASURVEYS__Question_Summary__c> lstSurveyQuesSum=new  list<TIMBASURVEYS__Question_Summary__c>();
     TIMBASURVEYS__Question_Summary__c oQuesSum=null;
     
     TIMBASURVEYS__QuestionType__c oQuesType=null;
     set<id> setAnswOppId=new  set<id>();  
     set<id> setSurveyId=new set<id>();
     
     for(TIMBASURVEYS__SurveyResponse__c res:lstTriggerNew)
     {
       
       sResId.add(res.id);
       //获取对应的AnswerOpptionId
        setAnswOppId.add(res.TIMBASURVEYS__AnswerOption__c);
       
       //获取对应的SurveyId 15位id
        setSurveyId.add(res.SurveyId__c);
       
       //获取对应的RecipientId
        setRec.add(res.TIMBASURVEYS__Recipient__c);
        
     } 
     
     if(setSurveyId.size()>0){
        //帅选QuestionType__c,同一个问题的同一个答案选项只有一条记录     
        list<TIMBASURVEYS__QuestionType__c> lstQTypeTemp=[select id,TIMBASURVEYS__AnswerOption__c,
            TIMBASURVEYS__SurveyQuestion__c,TIMBASURVEYS__TimesSelected__c 
          from TIMBASURVEYS__QuestionType__c where TIMBASURVEYS__AnswerOption__c in: setAnswOppId];  
          
        Map<string,TIMBASURVEYS__QuestionType__c> mapQuesType=new  Map<string,TIMBASURVEYS__QuestionType__c>();
        string strKey2='';
        for(TIMBASURVEYS__QuestionType__c qt:lstQTypeTemp)
        {
             strKey2=string.valueOf(qt.TIMBASURVEYS__SurveyQuestion__c)+string.valueOf(qt.TIMBASURVEYS__AnswerOption__c);
             mapQuesType.put(strKey2,qt);//一个question的AnswerOption__c对应唯一条QuestionType记录
        }
        lstQTypeTemp.clear();
        //存放新增QuestionType
        map<string,TIMBASURVEYS__QuestionType__c> mapInsertQuestionType=new map<string,TIMBASURVEYS__QuestionType__c>();   
        
        //查询对应的Survey      
        TIMBASURVEYS__Survey__c oSurvey=null;
        Map<id,TIMBASURVEYS__Survey__c> mapSurvey=new Map<id,TIMBASURVEYS__Survey__c>((list<TIMBASURVEYS__Survey__c>)
             [select id,Name,TIMBASURVEYS__CollectedResponses__c,TIMBASURVEYS__CollectingData__c,TIMBASURVEYS__Status__c from TIMBASURVEYS__Survey__c
             where id in:setSurveyId]);
         //一个survey同一个recipient只更新一次响应数
         //map<string,TIMBASURVEYS__SurveyResponse__c> mapSurReci=new map<string,TIMBASURVEYS__SurveyResponse__c>();
         
         //list<TIMBASURVEYS__SurveyResponse__c> lstResAll=[select id,SurveyId__c,TIMBASURVEYS__Recipient__c from  TIMBASURVEYS__SurveyResponse__c
             //where SurveyId__c in:setSurveyId and TIMBASURVEYS__Recipient__c in:setRec and id not in:sResId];
             
         //string strKey='';    
         //for(TIMBASURVEYS__SurveyResponse__c r:lstResAll){
             //strKey=string.valueOf(r.SurveyId__c)+string.valueOf(r.TIMBASURVEYS__Recipient__c);
             //mapSurReci.put(strKey,r);        
         //}
              
        
         //本次答案对应的问卷Id
         string strSurId='';
         string strRoleName=[select name from Profile  where id=:userInfo.getProfileId() limit 1 ].Name;
         string strUserName=userInfo.getName();
         string ouletName='';
         
         for(TIMBASURVEYS__SurveyResponse__c surRes:lstTriggerNew)
         {
              //strKey=string.valueOf(surRes.SurveyId__c)+string.valueOf(surRes.TIMBASURVEYS__Recipient__c);
              //一个survey同一个recipient只更新一次响应数
              //if(!mapSurReci.containsKey(strKey)){
                  //处理Survey
                  //oSurvey=mapSurvey.get(surRes.SurveyId__c);
                  //oSurvey.TIMBASURVEYS__CollectedResponses__c=  oSurvey.TIMBASURVEYS__CollectedResponses__c+1;//累加1
                  
                   //if(!oSurvey.TIMBASURVEYS__CollectingData__c)
                  //{
                      //oSurvey.TIMBASURVEYS__CollectingData__c=true;
                  //}
                   //if(oSurvey.TIMBASURVEYS__Status__c!='Open')
                   //{
                      //oSurvey.TIMBASURVEYS__Status__c='Open';
                  //}            
              //}
                 
            
            //每一个答案都会在SurveySumarry新增一条记录
            oSummary=new TIMBASURVEYS__Survey_Summary__c();
            oSummary.TIMBASURVEYS__Recipient__c=surRes.TIMBASURVEYS__Recipient__c;
            oSummary.TIMBASURVEYS__Survey__c=surRes.SurveyId__c;
            oSummary.TIMBASURVEYS__Complete_Survey_Name__c=mapSurvey.get(surRes.SurveyId__c).Name;
            //格式：PDA Submit（Role：Name;outletName：店面名称）
            ouletName=surRes.OutletName__c;
            string strResult='PDA Submit（'+strRoleName+':'+strUserName+';outletName：'+ouletName+')';
            if(strResult.length()>80)
            {
               strResult=strResult.substring(0,79);
            }
            oSummary.NAME=strResult;
           
            lstSurveySum.add(oSummary);
            
            //每一个答案都会在QuestionSumarry新增一条记录
            oQuesSum=new TIMBASURVEYS__Question_Summary__c();
            oQuesSum.TIMBASURVEYS__Answer__c=surRes.OptionValue__c;
            oQuesSum.TIMBASURVEYS__Question__c=surRes.TIMBASURVEYS__SurveyQuestion__c;
            oQuesSum.NAME=surRes.TIMBASURVEYS__SurveyQuestion__r.Name; 
            lstSurveyQuesSum.add(oQuesSum);
            
            //处理QuestionType，TIMBASURVEYS__TimesSelected__c是累加
            strKey2=string.valueOf(surRes.TIMBASURVEYS__SurveyQuestion__c)+string.valueOf(surRes.TIMBASURVEYS__AnswerOption__c);
            if(mapQuesType.containsKey(strKey2))//如果这个答案选项已经存在记录
            {
               oQuesType=mapQuesType.get(strKey2);
               oQuesType.TIMBASURVEYS__TimesSelected__c= oQuesType.TIMBASURVEYS__TimesSelected__c+1;
               //string strAnsOptId=surRes.TIMBASURVEYS__AnswerOption__c;
               //mapQuesType.put(strAnsOptId, oQuesType);
            }
            else
            {
               //新增的数据放在新增的map里面，因为新增的数据答案选项有可能重复
               if(mapInsertQuestionType.containsKey(strKey2)){
                   oQuesType=mapInsertQuestionType.get(strKey2);
                    oQuesType.TIMBASURVEYS__TimesSelected__c=oQuesType.TIMBASURVEYS__TimesSelected__c+1;//累加为1               
               }else{
                    oQuesType=new TIMBASURVEYS__QuestionType__c();
                    oQuesType.TIMBASURVEYS__AnswerOption__c=surRes.TIMBASURVEYS__AnswerOption__c;
                    oQuesType.TIMBASURVEYS__SurveyQuestion__c=surRes.TIMBASURVEYS__SurveyQuestion__c;
                    oQuesType.TIMBASURVEYS__TimesSelected__c=1;//第一次累加为1
                    //把新增的TIMBASURVEYS__QuestionType__c加入到map中
                    mapInsertQuestionType.put(strKey2,oQuesType);           
               }
    
            }
              
           } 
           
           //更新Survey主对象
            //update mapSurvey.values();
           
           
           //处理Recipient
           list<TIMBASURVEYS__Recipient__c> lstReci=[select id, TIMBASURVEYS__Collected__c from TIMBASURVEYS__Recipient__c
                where id in:setRec]; 
           for(TIMBASURVEYS__Recipient__c rc:lstReci)
           {
              rc.TIMBASURVEYS__Collected__c=true;
           }
    
          //更新recipient
          update lstReci;
          
          
          //处理survey
         list<TIMBASURVEYS__Recipient__c> lRecipientResponse=new list<TIMBASURVEYS__Recipient__c>();
         lRecipientResponse=[select id, TIMBASURVEYS__Collected__c,TIMBASURVEYS__Survey__c from TIMBASURVEYS__Recipient__c
                where TIMBASURVEYS__Survey__c in:setSurveyId and TIMBASURVEYS__Collected__c=:true];
         //先全部置0       
         for(TIMBASURVEYS__Survey__c s:mapSurvey.values()){
             s.TIMBASURVEYS__CollectedResponses__c=0;
             s.TIMBASURVEYS__CollectingData__c=true;
             s.TIMBASURVEYS__Status__c='Open';
             
         }
         for(TIMBASURVEYS__Recipient__c r:lRecipientResponse){
             if(mapSurvey.containsKey(r.TIMBASURVEYS__Survey__c)){
                 oSurvey=mapSurvey.get(r.TIMBASURVEYS__Survey__c); 
                 oSurvey.TIMBASURVEYS__CollectedResponses__c=oSurvey.TIMBASURVEYS__CollectedResponses__c+1;              
             }    
         }
         update  mapSurvey.values();       
           
           //SurveySurmmary
           insert lstSurveySum;
           //QuestionSurmmary
           for(integer i=0;i<lstSurveySum.size();i++)
           {
              oSummary=lstSurveySum[i];
              oQuesSum= lstSurveyQuesSum[i];
              
              oQuesSum.TIMBASURVEYS__Survey_Summary__c=oSummary.id;
           }
           insert lstSurveyQuesSum;
           
           //update QuesType
           update mapQuesType.values();
           if(mapInsertQuestionType.size()>0){
               insert mapInsertQuestionType.values();    
           }     
     }


 }
}